package com.AddressListModule.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import util.Constant;
import util.UtilDAO;

import com.AddressListModule.bean.ContactBean;
import com.AddressListModule.bean.DirectoryBean;
import com.AddressListModule.bean.TbUserDirectory;
import com.AddressListModule.dao.AddressModuleDao;
import com.AddressListModule.dao.TbUserDirectoryDAO;
import com.UserModule.bean.TbUserBean;
import com.UserModule.dao.*;
import com.jspsmart.upload.SmartUpload;

public class AddressModuleServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		String method= request.getParameter("method");//get the name of method
		PrintWriter out = response.getWriter();
		TbUserBean user = (TbUserBean)request.getSession().getAttribute("User"); 
		if(method.equals("lookDir")){//show the record of directory
			StringBuffer sb = new StringBuffer();
			String dirName = Constant.decodeString(request.getParameter("dirName"));
			String dingwei = request.getParameter("dingwei");
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", String.valueOf(user.getUserId()), "");//get the first level directory
			sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onClick=\"$.msgbox({height:510,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人列表'})\" value=\""+directoryBean.getDirName()+"\" >"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+dingwei);
		}else if(method.equals("addDir")){//add a directory
			String dirId = request.getParameter("dirId");
			String dirLevel = request.getParameter("dirLevel");//the level of the directory
			//request.getSession().setAttribute("dirId", dirId);
			response.sendRedirect("AddressListModule/addDirectory.jsp?dirId="+dirId+"&dirLevel="+dirLevel);
		}else if(method.equals("addDirSubmit")){
			StringBuffer sb = new StringBuffer();
			String fatherDirId = request.getParameter("fatherDirId");
			String dirLevel = request.getParameter("dirLevel");//the level of a directory
			String dirName = Constant.decodeString(request.getParameter("dirName"));
			DirectoryBean addDirectoryBean = new DirectoryBean();
			addDirectoryBean.setCreateTime(Constant.decodeDate());
			addDirectoryBean.setDirLevel(Integer.parseInt(dirLevel)+1);
			addDirectoryBean.setDirName(dirName);
			addDirectoryBean.setParentDirId(Integer.parseInt(fatherDirId));
			addDirectoryBean.setUserId(user.getUserId());
			boolean bo = new AddressModuleDao().add(addDirectoryBean);
			if(bo){
				fatherDirId = String.valueOf(new AddressModuleDao().getDirectory("tb_directory"));
			}
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", "","");//get the first level directory

			//sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=modifyDir&dirLevel=-1&dirId=0\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=deleteDir&dirLevel=-1&dirId=0\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span> <ul>");

			sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=modifyDir&dirLevel=-1&dirId=0\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=deleteDir&dirLevel=-1&dirId=0\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span> <ul>");

			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onclick=\"$mb=$.msgbox({height:400,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人'});\">"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+fatherDirId);
		}else if(method.equals("modifyDir")){//modify a directory
			int num = new Random().nextInt(10000);
			String dirLevel = request.getParameter("dirLevel");
			String dirId = request.getParameter("dirId");
			DirectoryBean directoryBean = new AddressModuleDao().getDirectory("tb_directory", "dirId", Integer.parseInt(dirId));
			request.getSession().setAttribute("directoryBean", directoryBean);
			response.sendRedirect("AddressListModule/modifyDirectory.jsp?dirLevel="+dirLevel+"&num="+num);
		}else if(method.equals("modifyDirSubmit")){//the submit of the modify
			StringBuffer sb = new StringBuffer();
			String dirLevel = request.getParameter("dirLevel");
			String dirId = request.getParameter("dirId");
			String dirName = Constant.decodeString(request.getParameter("dirName"));
			DirectoryBean dirBean = new DirectoryBean();
			dirBean.setDirId(Integer.parseInt(dirId));
			dirBean.setDirName(dirName);
			boolean bo = new AddressModuleDao().modifyDirectory(dirBean);
			
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", "","");//get the first level directory
			sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=modifyDir&dirLevel=-1&dirId=0\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'../AddressServlet?method=deleteDir&dirLevel=-1&dirId=0\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span> <ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onclick=\"$mb=$.msgbox({height:400,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人'});\">"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+dirId);
			
		}else if(method.equals("deleteDir")){//delete a directory
			String dirLevel = request.getParameter("dirLevel");
			String dirId = request.getParameter("dirId");
			int num = new Random().nextInt(10000);
			DirectoryBean directoryBean = new AddressModuleDao().getDirectory("tb_directory", "dirId", Integer.parseInt(dirId));
			request.getSession().setAttribute("directoryBean", directoryBean);
			response.sendRedirect("AddressListModule/deleteDirectory.jsp?dirLevel="+dirLevel+"&num="+num);
			
		}else if(method.equals("deleteDirSubmit")){//the submit of delete a directory
			StringBuffer sb = new StringBuffer();
			String dirId = request.getParameter("dirId");
			String fatherId  = request.getParameter("fatherId");
			boolean fatherBo = UtilDAO.del("tb_directory", "dirId", dirId);
			if(fatherBo){
				String conditionChil = " parentDirId="+dirId;
				String chilDirId = new AddressModuleDao().getChilDirId("tb_directory", "", conditionChil);
				if(!chilDirId.equals("")){
					chilDirId=chilDirId+dirId;
				}else{
					chilDirId=dirId;
				}
				String condition=" tb_directory where parentDirId="+dirId;
				boolean chilBo = UtilDAO.delAll(condition);
				String conditionTT=" tb_customer where dirId in("+chilDirId+")";
				boolean contentBo = UtilDAO.delAll(conditionTT);
			}
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", "", "");//get the first level directory
			sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onclick=\"$mb=$.msgbox({height:400,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人'});\">"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+fatherId);
		}else if(method.equals("selectDir")){//select the Directory when send mms or sms
			List<DirectoryBean> lastDirList = new ArrayList<DirectoryBean>();
			String formName = request.getParameter("formName");
			String selectPhone = request.getParameter("selectPhone");
			TbUserBean tbUser =(TbUserBean)request.getSession().getAttribute("User");//get the loginUser
			int userState = tbUser.getUserState();//the state of loginUser  0:超级管理员1:管理员2：普通用户3:审核人员
			StringBuffer sb = new StringBuffer();
			List<DirectoryBean> topDirectoryList =new ArrayList<DirectoryBean>();
			if(userState==0){
				topDirectoryList =this.getTopDirectryList("", "", "");//get the first level directory
			}else if(userState>0){
				String tiaojian="";
				String tiaojianRole="";
				tiaojian="userId="+tbUser.getUserId();
				
				List<DirectoryBean> dirList = this.getDirectryList(tiaojian);
				if(dirList!=null&&dirList.size()>0){
					for(DirectoryBean db:dirList){
						tiaojianRole+=db.getDirId()+",";
					}
					tiaojianRole=tiaojianRole.substring(0,tiaojianRole.length()-1);
				}
				List<TbUserDirectory> userDirList = this.getDirectryListByRole(tiaojianRole);//get the object from tb_userDirectory table
				if(dirList!=null&&dirList.size()>0){
					topDirectoryList = dirList;
				}
				if(userDirList!=null&&userDirList.size()>0){
					for(TbUserDirectory tbUserDir:userDirList){
						DirectoryBean dirB = this.getDirectory("tb_directory", "dirId", tbUserDir.getDirectoryId());
						if(!topDirectoryList.contains(dirB)){
							topDirectoryList.add(dirB);
						}
					}
				}
			}
			sb.append(" <li class=\"0\"><a>通讯录</a><ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				List<String> dirLst = this.dirLst(topDirectoryList);
				
				if(dirLst!=null&&dirLst.size()>0){
					for(String vv:dirLst){
						DirectoryBean d = this.getDirectory("tb_directory", "dirId", Integer.parseInt(vv));
						lastDirList.add(d);
					}
				}
			}
			if(lastDirList!=null&&lastDirList.size()>0){
				for(int i = 0;i<lastDirList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)lastDirList.get(i);
					sb.append(" <li><input type=\"checkbox\" name=\"select_Dir\" id=\""+directoryBean.getDirId()+"\" onclick=\"ts(this);\" /><a>"+directoryBean.getDirName()+"</a>");
					String chilDirectory = this.getDirStringByRole(null,directoryBean.getDirId(),request);
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			int num = new Random().nextInt(10000);
			response.sendRedirect("SmsModule/selectDirectory.jsp?num="+num+"&formName="+formName+"&selectPhone="+selectPhone);
		}else if(method.equals("getLinkMan")){//get the linkMan by directoryId
			String dirId = request.getParameter("dirId");
			int num = new Random().nextInt(10000);
			String Page = request.getParameter("Page");
			List<ContactBean> contactList = this.getLinkMan(Integer.parseInt(dirId));
			request.getSession().setAttribute("contactList", contactList);
			response.sendRedirect("AddressListModule/ShowContact.jsp?dirId="+dirId+"&num="+num+"&Page="+Page);
		}else if(method.equals("addLinkMan")){//add a linkMan
			String dirId = request.getParameter("dirId");
			String createTime = Constant.decodeDateT();
			response.sendRedirect("AddressListModule/addContact.jsp?dirId="+dirId+"&createTime="+createTime);
		}else if(method.equals("addContactSubmit")){//the submit add a linkMan
			StringBuffer sb = new StringBuffer();
			String linkName = Constant.decodeString(request.getParameter("linkName"));
			String linkPhone = request.getParameter("linkPhone");
			String dirId = request.getParameter("dirId");
			String jifen = request.getParameter("jifen");
			//Date birthday = Timestamp.valueOf(linkBirth);
			ContactBean contactBean = new ContactBean();
			contactBean.setCustomerIntegral("0");
			contactBean.setCustomerName(linkName);
			contactBean.setCustomerPhone(linkPhone);
			contactBean.setLastTime(Constant.decodeDate());
			contactBean.setDirId(Integer.parseInt(dirId));
			contactBean.setCustomerIntegral(jifen);
			boolean bo = new AddressModuleDao().addContact(contactBean);
			
			
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", "", "");//get the first level directory
			sb.append(" <li class=\"0\"><a>通讯录</a> <span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onclick=\"$mb=$.msgbox({height:400,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人'});\">"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+dirId);
			
		}else if(method.equals("modifyContact")){//modify a linkMan
			System.out.println("come here !!!!!!");
			String contactId = request.getParameter("mmsId");
			System.out.println("come here !!!!!!"+contactId);
			int num = new Random().nextInt(10000);
			System.out.println("come here !!!!!!");
			ContactBean contactBean = new AddressModuleDao().getContact(contactId);
			System.out.println("come here !!!!!!");
			List<ContactBean> contactList=new ArrayList<ContactBean>();
			System.out.println("aaa");
			contactList.add(contactBean);
			System.out.println("bbbb");
			request.getSession().setAttribute("contact_Bean", contactBean);
			System.out.println("cccc");
			response.sendRedirect("AddressListModule/modifyContact.jsp?num="+num);
			System.out.println("ddddd");
		}else if(method.equals("modifyContactSubmit")){//modify a linkMan submit
			int num = new Random().nextInt(10000);
			String linkMan = Constant.decodeString(request.getParameter("linkName"));
			String linkPhone = Constant.decodeString(request.getParameter("linkPhone"));
			String contactId = request.getParameter("mmsId");
			String customerIntegral=request.getParameter("customerIntegral");//积分
			ContactBean contactBean = new ContactBean();
			contactBean.setCustomerId(Integer.parseInt(contactId));
			contactBean.setCustomerIntegral(customerIntegral);
			contactBean.setCustomerName(linkMan);
			contactBean.setCustomerPhone(linkPhone);
			contactBean.setLastTime(Constant.decodeDate());
			String condition = "1=1";
			if(!linkPhone.equals("")){
				condition+=" and customerPhone='"+linkPhone+"'";
			}
			if(!linkMan.equals("")){
				condition+=" and customerName like '%"+linkMan+"%'";
			}
			boolean bo = new AddressModuleDao().modifyContact(contactBean);
			List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", condition);
			request.getSession().setAttribute("phone", linkPhone);
			request.getSession().setAttribute("linkName", linkMan);
			request.getSession().setAttribute("contactList", contactList);
			response.sendRedirect("AddressListModule/contactList.jsp?num="+num);
			
		}else if(method.equals("lookContact")){//show the contact
			String linkName = Constant.decodeString(request.getParameter("linkName"));
			String page = request.getParameter("Page");
			String phone = request.getParameter("phone");
			String condition = "1=1";
			if(!phone.equals("")){
				condition+=" and customerPhone='"+phone+"'";
			}
			if(!linkName.equals("")){
				condition+=" and customerName like '%"+linkName+"%'";
			}
			List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", condition);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("linkName", linkName);
			request.getSession().setAttribute("contactList", contactList);
			request.getSession().setAttribute("resultAdd", "");
			response.sendRedirect("AddressListModule/contactList.jsp?Page="+page);
		}else if(method.equals("searchContact")){//search the contact
			String linkName = Constant.decodeString(request.getParameter("linkName"));
			String phone = request.getParameter("phone");
			String condition = "1=1";
			if(!phone.equals("")){
				condition+=" and customerPhone='"+phone+"'";
			}
			if(!linkName.equals("")){
				condition+=" and customerName like '%"+linkName+"%'";
			}
			List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", condition);
			request.getSession().setAttribute("linkName", linkName);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("contactList", contactList);
			response.sendRedirect("AddressListModule/contactList.jsp");
		}else if(method.equals("deleteAllContact")){
			String mmsId = request.getParameter("idList");
			mmsId = mmsId.substring(0, mmsId.length()-1);
			String linkName = Constant.decodeString(request.getParameter("linkName"));
			String phone = request.getParameter("phone");
			String condition = " tb_customer where customerId in("+mmsId+")";
			boolean bo = UtilDAO.delAll(condition);
			String conditionT = "1=1";
			if(!phone.equals("")){
				conditionT+=" and customerPhone='"+phone+"'";
			}
			if(!linkName.equals("")){
				conditionT+=" and customerName like '%"+linkName+"%'";
			}
			List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", conditionT);
			request.getSession().setAttribute("linkName", linkName);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("contactList", contactList);
			request.getSession().setAttribute("resultAdd", "删除成功");
			response.sendRedirect("AddressListModule/contactList.jsp");
		}else if(method.equals("deleteContact")){//delete a contact
			String mmsId = request.getParameter("mmsId");
			String linkName = Constant.decodeString(request.getParameter("linkName"));
			String phone = request.getParameter("phone");
			boolean bo = UtilDAO.del("tb_customer", "customerId", Integer.parseInt(mmsId));
			String conditionT = "1=1";
			if(!phone.equals("")){
				conditionT+=" and customerPhone='"+phone+"'";
			}
			if(!linkName.equals("")){
				conditionT+=" and customerName like '%"+linkName+"%'";
			}
			List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", conditionT);
			request.getSession().setAttribute("linkName", linkName);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("contactList", contactList);
			request.getSession().setAttribute("resultAdd", "删除成功");
			response.sendRedirect("AddressListModule/contactList.jsp");
		}else if(method.equals("getLinkMans")){//get the linkMans when you send a sms or an mms
			System.out.println("进入!");
			String formName=request.getParameter("formName");
			String selectPhone = request.getParameter("selectPhone");
			String dirId = request.getParameter("dirId");
			dirId = dirId.substring(0, dirId.length()-1);
			
			String condition="dirId in("+dirId+")";
			
			List<ContactBean> linkMans  = this.getLinkMans(condition);
			StringBuffer sb = new StringBuffer();
			sb.append(formName+",");
			sb.append(selectPhone+",");
			if(linkMans!=null&&linkMans.size()>0){
				for(ContactBean contact: linkMans){
					sb.append("<option>"+contact.getCustomerPhone()+"</option>");
				}
			}else{
				//sb.append("");
			}
			out.println(sb.toString());
		}else if(method.equals("importLinkMan")){
			String dirId = request.getParameter("dirId");
			int num = new Random().nextInt(10000);
			response.sendRedirect("AddressListModule/importContact.jsp?dirId="+dirId+"&num="+num);
		}else if(method.equals("importContactSubmit")){
			String dirId = request.getParameter("dirId");
			String realImagePath = Constant.decodeString(request
					.getParameter("realImagePath"));
			String tempPath = request.getRealPath("/")
					+ "upload".replace("\\", "/");
			String result = "";
			File file = new File(tempPath);
			if(!file.isFile()){
				file.mkdirs();
			}
			// new a SmartUpload object
			SmartUpload su = new SmartUpload();
			System.out.println("aaaa");
			su.setCharset("UTF-8");// 使用时候最关键的一步
			// 上传初始化
			System.out.println("bbbbbbb");
			su.initialize(this.getServletConfig(),request,response);
			System.out.println("ccccccc");

			// 设定上传限制
			// 1.限制每个上传文件的最大长度。
			su.setMaxFileSize(10000000);
			System.out.println("dddddd");
			// 2.限制总上传数据的长度。
			su.setTotalMaxFileSize(20000000);
			System.out.println("fffffffff");
			// 3.设定允许上传的文件（通过扩展名限制）,仅允许doc,txt文件。
			su.setAllowedFilesList("xls,XLS");
			System.out.println("eeeeeeee");
			boolean sign = true;
			System.out.println("gggggggg");
			// 4.设定禁止上传的文件（通过扩展名限制）,禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
			try {
				su.setDeniedFilesList("exe,bat,jsp,htm,html");
				System.out.println("hhhhhhhh");
				// 上传文件
				su.upload();
				System.out.println("iiiiiiii");
				// 将上传文件保存到指定目录
				tempPath = tempPath.replace("\\", "/");
				System.out.println("jjjjjjjjjj");
				tempPath = tempPath + "/";
				System.out.println("kkkkkkkkkk");
				
				su.save(tempPath);
				System.out.println("文件名称"+su.getFiles().getFile(0).getFileName());
				tempPath += "\\" + su.getFiles().getFile(0).getFileName();
				tempPath = tempPath.replace("\\", "/");
				tempPath = tempPath.replace("//", "/");
				System.out.println("tempsssssssssssssPath==="+tempPath);
				
				
				Workbook wb;
				InputStream is2 = new FileInputStream(tempPath);
				wb = Workbook.getWorkbook(is2);
				Sheet sheet = wb.getSheet(0);
				int row = sheet.getRows();
				List<ContactBean> contactList = new ArrayList<ContactBean>();
				int maxId = new AddressModuleDao().getMaxContantId();//在导入时把联系人表中取得ID最大值
				//// 表结构是：姓名：手机号码：积分
				for(int i = 1;i<row;i++){
					Cell firstCell = sheet.getCell(0, i);
					String firstContent = firstCell.getContents();
					Cell secondCell = sheet.getCell(1,i);
					String secondContent = secondCell.getContents();
					
					ContactBean contactBean = new ContactBean();
					contactBean.setCustomerIntegral("0");
					contactBean.setCustomerName(firstContent);
					contactBean.setCustomerPhone(secondContent);
					contactBean.setDirId(Integer.parseInt(dirId));
					contactBean.setLastTime(Constant.decodeDate());
					contactList.add(contactBean);
				}
				if(contactList!=null&&contactList.size()>0){
					boolean bo = new AddressModuleDao().saveContactList(contactList);
					if(bo){
						result="导入成功";
					}else{
						result="导入失败";
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			StringBuffer sb = new StringBuffer();
			List<DirectoryBean> topDirectoryList = this.getTopDirectryList("", "", "");//get the first level directory
			sb.append(" <li class=\"0\"><a>通讯录</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><ul>");
			if(topDirectoryList!=null&&topDirectoryList.size()>0){
				for(int i = 0;i<topDirectoryList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)topDirectoryList.get(i);
					sb.append(" <li class=\""+directoryBean.getDirId()+"\"><a onClick=\"$.msgbox({height:510,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&dirId="+directoryBean.getDirId()+"'},title: '联系人列表'})\" value=\""+directoryBean.getDirName()+"\" >"+directoryBean.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'删除通讯\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+directoryBean.getDirLevel()+"&dirId="+directoryBean.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
					String chilDirectory = this.getDirStringShow(null,directoryBean.getDirId());
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("AddressListModule/showDirectry.jsp?dingwei="+dirId);
		}else if(method.equals("givesDir")){//在新建用户时对其进行通讯录赋予
			List<DirectoryBean> lastDirList = new ArrayList<DirectoryBean>();
			String formName = request.getParameter("formName");
			String selectPhone = request.getParameter("selectPhone");
			String user_Id = request.getParameter("userId");
			TbUserBean tbUser =new TbUserDAO().getTbUsersByUserId(Integer.parseInt(user_Id));//get the loginUser
			System.out.println("user_Id=="+user_Id);
			int userState = tbUser.getUserState();//the state of loginUser  0:超级管理员1:管理员2：普通用户3:审核人员
			
			StringBuffer sb = new StringBuffer();
			List<DirectoryBean> topDirectoryList =new ArrayList<DirectoryBean>();
			List<DirectoryBean> directoryList =new ArrayList<DirectoryBean>();
			
			String tiaojian="userId="+user_Id;
			List<DirectoryBean> dirList = this.getDirectryList(tiaojian);
			List<TbUserDirectory> userDirList = this.getDirectryListByRole(tiaojian);//get the object from tb_userDirectory table
			if(dirList!=null&&dirList.size()>0){
				directoryList = dirList;
			}
			System.out.println("to heree!!!!!!!!!");
			if(userDirList!=null&&userDirList.size()>0){
				for(TbUserDirectory tbUserDir:userDirList){
					DirectoryBean dirB = this.getDirectory("tb_directory", "dirId", tbUserDir.getDirectoryId());
					if(!directoryList.contains(dirB)){
						directoryList.add(dirB);
					}
				}
			}
			System.out.println("aaaaaaaabbbbbbb"+directoryList.size());
			if(userState==0||userState==1){
				System.out.println("superadmin!!!");
				topDirectoryList =this.getTopDirectryListUser("", "", "");//get the first level directory
				
			
			}else if(userState>1){
				System.out.println("user come in!!!!!");
				//topDirectoryList = directoryList;
			
				
				String tiaojianT="userId="+tbUser.getCreateUserId();
				
				List<DirectoryBean> dirListT = this.getDirectryList(tiaojianT);
				List<TbUserDirectory> userDirListT = this.getDirectryListByRole(tiaojianT);//get the object from tb_userDirectory table
				if(dirListT!=null&&dirListT.size()>0){
					for(int x = 0;x<dirListT.size();x++){
						if(!topDirectoryList.contains(dirListT.get(x))){
							topDirectoryList.add(dirListT.get(x));
						}
						
					}
				}
				System.out.println("aaaaaaaabbbbbbbTTT"+directoryList.size());
				if(userDirListT!=null&&userDirListT.size()>0){
					System.out.println("1111111＝＝＝"+directoryList.size());
					for(TbUserDirectory tbUserDir:userDirListT){
						System.out.println("112222222＝＝"+directoryList.size());
						DirectoryBean dirBT = this.getDirectory("tb_directory", "dirId", tbUserDir.getDirectoryId());
						System.out.println("33333333＝＝"+directoryList.size());
						if(!topDirectoryList.contains(dirBT)){
							System.out.println("4444444＝＝"+directoryList.size());
							topDirectoryList.add(dirBT);
						}
					}
				}
				
				System.out.println("aaaaaaaabbbbTTTTTTTTRRRRRRRRRbbb"+directoryList.size());
				System.out.println("to heree!!!!!!!!!");
				System.out.println("sizdddddde==="+topDirectoryList.size());
			}
			
			List<String> DirIdList = new ArrayList<String>();
			if(directoryList!=null&&directoryList.size()>0){
				System.out.println("commmmmmmmmmmmmmmmmmmmmmmmm"+directoryList.get(0).getDirName());
				for(int y=0;y<directoryList.size();y++){
					System.out.println("ID号"+directoryList.get(y).getDirId());
					DirIdList.add(String.valueOf(directoryList.get(y).getDirId()));
				}
			}
			for(String str:DirIdList){
				System.out.println("strIDDDDDDDDD==="+str);
			}
			System.out.println("size========="+topDirectoryList.size());
			sb.append(" <li class=\"0\"><a>通讯录</a> <span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel=-1&dirId=0\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><ul>");
			
			List<String> dirLst = this.dirLst(topDirectoryList);
		
			if(dirLst!=null&&dirLst.size()>0){
				for(String vv:dirLst){
					DirectoryBean d = this.getDirectory("tb_directory", "dirId", Integer.parseInt(vv));
					lastDirList.add(d);
				}
			}
		
			if(lastDirList!=null&&lastDirList.size()>0){
				for(int i = 0;i<lastDirList.size();i++){
					DirectoryBean directoryBean =(DirectoryBean)lastDirList.get(i);
					String checkStr="";
					if(DirIdList.contains(String.valueOf(directoryBean.getDirId()))){
						checkStr="checked";
					}
					sb.append(" <li><input type=\"checkbox\" "+checkStr+" name=\"select_Dir\" id=\""+directoryBean.getDirId()+"\" onclick=\"ts(this);\" /><a>"+directoryBean.getDirName()+"</a>");
					String chilDirectory = this.getDirStringByRoleUser(null,directoryBean.getDirId(),request,DirIdList);
					sb.append(chilDirectory);
					sb.append("</li>");
				}
				//sb.append("</li>");
			}
			sb.append("</ul></li>");
			request.getSession().setAttribute("sb", sb);
			int num = new Random().nextInt(10000);
			System.out.println("====givesDir====formName==="+formName+"======"+selectPhone);
			response.sendRedirect("UserModule/selectDirectory.jsp?num="+num+"&formName="+formName+"&selectPhone="+selectPhone+"&userId="+user_Id);
		}else if(method.equals("givesDirSubmit")){//新建用户时对其进行通讯录赋值的提交操作
			System.out.println("提交");
			String dirId = request.getParameter("dirId");
			String userId = request.getParameter("userId");
			System.out.println(userId+"===="+dirId);
			List<TbUserDirectory> userDirList = new ArrayList<TbUserDirectory>();
			if(!dirId.equals("")){
				String dir_Id[] = dirId.split(",");
				for(int z=0;z<dir_Id.length;z++){
					TbUserDirectory userDir = new TbUserDirectory();
					userDir.setCreateDate(Constant.decodeDate());
					userDir.setDirectoryId(Integer.parseInt(dir_Id[z]));
					userDir.setUserId(Integer.parseInt(userId));
					userDirList.add(userDir);
				}
				boolean bbo = new TbUserDirectoryDAO().deleteUserDir(Integer.parseInt(userId));
				boolean bo = new TbUserDirectoryDAO().saveUserDirList(userDirList);
				
			}
		}else if(method.equals("getDirByParId")){
			//response.setCharacterEncoding("UTF-8");
			StringBuffer sb = new StringBuffer();
			String parDirId = request.getParameter("parDir");
			String nextDir = request.getParameter("nextDir");
			sb.append(nextDir+",");
			String condition="userId="+user.getUserId();
			List<TbUserDirectory> userDirectoryList = new AddressModuleDao().getUserDirByRole("tb_userDirectory","createDate",condition);
			if(userDirectoryList!=null&&userDirectoryList.size()>0){
				for(TbUserDirectory userDir:userDirectoryList){
					parDirId+=","+userDir.getDirectoryId();
				}
			}
			String conditionDir = "parentDirId in("+parDirId+")";
			List<DirectoryBean> dirList = new AddressModuleDao().getAllTbDirectory("tb_directory", "createTime", conditionDir);
			if(dirList!=null&&dirList.size()>0){
				for(DirectoryBean db:dirList){
					sb.append("<option value=\""+db.getDirId()+"\">"+db.getDirName()+"</option>");
				}
			}
			System.out.println("sb===="+sb.toString());
			out.println(sb.toString());
		}
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:在新建用户时对其进行通讯录赋值时的操作
	 * @param dirName
	 * @param startTime
	 * @param endTime
	 * @return
	 *2011-3-4
	 *上午09:08:13
	 *UserModule
	 *com.AddressListModule.servlet
	 *List<DirectoryBean>
	 */
	public List<DirectoryBean> getTopDirectryListUser(String dirName,String startTime,String endTime ){
		List<DirectoryBean> directoryList = new ArrayList();
		String condition = "";
		if(!dirName.equals("")){
			condition+=" and dirName like'%"+dirName+"%'";
		}
		directoryList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		return directoryList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:在用户表中对一个用户的通讯录群组进行修改
	 * @param sb
	 * @param parDirId
	 * @param request
	 * @return
	 *2011-3-3
	 *下午04:18:12
	 *UserModule
	 *com.AddressListModule.servlet
	 *String
	 */
	public String getDirStringByRoleUser(StringBuffer sb,int parDirId,HttpServletRequest request,List<String> str){
		System.out.println("come heree!!!!!!====!!!"+str.size());
		StringBuffer sssb = null;
		if(sb==null){
			sssb = new StringBuffer();
		}else{
			sssb = sb ;
		}
		String condition = "parentDirId="+parDirId;
		System.out.println("condition=-"+condition);
		List<DirectoryBean> dirList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		if(dirList!=null&&dirList.size()>0){
			sssb.append(" <ul class=\"treeview\">");
			for(DirectoryBean dir:dirList){
				//sbALL.append(" <ul class=\"treeview\">");
				String strcheck="";
				if(str.contains(String.valueOf(dir.getDirId()))){
					strcheck="checked";
				}
				sssb.append("<li><input type=\"checkbox\""+strcheck+" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
				/*if(tbUser.getUserState()==0){//superadmin
					sssb.append("<li><input type=\"checkbox\" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
				}else{
					if(commonDirList.contains(dir)){
						sssb.append("<li><input type=\"checkbox\" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
					}
					
				}*/
				this.getDirStringByRoleUser(sssb,dir.getDirId(),request,str);
				sssb.append("</li>");
			}
			sssb.append("</ul>");
		}else{
			//sbALL.append("</li>");
		}
		//sbALL.append("</li>");
		return sssb.toString();
	}
/**
 * 
 *@author qingyu zhang
 *@function:get the records by conditions
 * @param dirName
 * @param tiaojian
 * @return
 *2011-2-17
 *下午02:32:23
 *AddressListModule
 *com.AddressListModule.servlet
 *List<DirectoryBean>
 */
	public List<DirectoryBean> getDirectryList(String tiaojian ){
		List<DirectoryBean> directoryList = new ArrayList();
		String condition = "";
		condition=tiaojian;
		directoryList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		return directoryList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get the first level Directory
	 * @param dirName
	 * @param startTime
	 * @param endTime
	 * @return
	 *2011-2-17
	 *下午03:12:49
	 *AddressListModule
	 *com.AddressListModule.servlet
	 *List<DirectoryBean>
	 */
	public List<DirectoryBean> getTopDirectryList(String dirName,String startTime,String endTime ){
		List<DirectoryBean> directoryList = new ArrayList();
		
		String condition = "";
		condition+="dirLevel=0 ";
		if(!startTime.equals("")){
			condition+=" and userId="+startTime;
		}
		if(!dirName.equals("")){
			condition+=" and dirName like'%"+dirName+"%'";
		}
		directoryList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		return directoryList;
	}
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:have not the checkBox
	 * @param parDirId
	 * @return
	 *2011-2-19
	 *上午11:09:38
	 *AddressListModule
	 *com.AddressListModule.servlet
	 *String
	 */
	public String getDirStringShow(StringBuffer sb,int parDirId){
		StringBuffer ssb =null;
		if(sb==null){
			ssb = new StringBuffer();
		}else{
			ssb = sb;
		}
		String condition = "parentDirId="+parDirId;
		List<DirectoryBean> dirList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		if(dirList!=null&&dirList.size()>0){
			ssb.append(" <ul class=\"treeview\">");
			for(DirectoryBean dir:dirList){
				//sbALL.append(" <ul class=\"treeview\">");
				ssb.append("<li class=\""+dir.getDirId()+"\"><a onClick=\"$.msgbox({height:510,width:800,content:{type:'iframe',content:'./AddressServlet?method=getLinkMan&Page=1&Page=1&dirId="+dir.getDirId()+"'},title: '联系人列表'})\" value=\""+dir.getDirName()+"\" >"+dir.getDirName()+"</a><span title=\"添加通讯录\" class=\"addnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addDir&dirLevel="+dir.getDirLevel()+"&dirId="+dir.getDirId()+"\'},title:\'新增通讯录\',onAjaxed: function(data){}});\">添加通讯录</a></span><span title=\"编辑通讯录\" class=\"editnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=modifyDir&dirLevel="+dir.getDirLevel()+"&dirId="+dir.getDirId()+"\'},title:\'编辑通讯录\',onAjaxed: function(data){}});\">编辑通讯录</a></span><span title=\"删除通讯录\" class=\"delnode\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=deleteDir&dirLevel="+dir.getDirLevel()+"&dirId="+dir.getDirId()+"\'},title:\'删除通讯录\',onAjaxed: function(data){}});\">删除通讯录</a></span><span title=\"新增联系人\" class=\"addLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:400,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=addLinkMan&dirLevel="+dir.getDirLevel()+"&dirId="+dir.getDirId()+"\'},title:\'新增联系人\',onAjaxed: function(data){}});\">新增联系人</a></span><span title=\"导入联系人\" class=\"importLinkMan\"><a style=\"cursor:hand\" onclick=\"$mb=$.msgbox({height:220,width:500,content:{type:\'ajax\',content:\'./AddressServlet?method=importLinkMan&dirLevel="+dir.getDirLevel()+"&dirId="+dir.getDirId()+"\'},title:\'导入联系人\',onAjaxed: function(data){}});\">导入联系人</a></span>");
				this.getDirStringShow(ssb,dir.getDirId());
				ssb.append("</li>");
			}
			ssb.append("</ul>");
		}else{
			//sbALL.append("</li>");
		}
		//sbALL.append("</li>");
		return ssb.toString();
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get the linkMan by directoryId
	 * @param dirId
	 * @return
	 *2011-2-19
	 *下午02:46:09
	 *AddressListModule
	 *com.AddressListModule.servlet
	 *StringBuffer
	 */
	public List getLinkMan(int dirId){
		String condition ="dirId="+dirId;
		List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", condition);
		if(contactList==null){
			contactList = new ArrayList();
		}
		return contactList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get linkMans by dirIds (1,2,3,4....)
	 * @param dirIds
	 * @return
	 *2011-2-26
	 *下午03:15:42
	 *MmsModule
	 *com.AddressListModule.servlet
	 *List
	 */
	public List<ContactBean> getLinkMans(String tiaojian){
		String condition =tiaojian;
		List<ContactBean> contactList = new AddressModuleDao().getContactList("tb_customer", "lastTime", condition);
		if(contactList==null){
			contactList = new ArrayList();
		}
		return contactList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get the directory by role
	 * @param dirName
	 * @param tiaojian
	 * @return
	 *2011-2-25
	 *上午10:18:31
	 *MmsModule
	 *com.AddressListModule.servlet
	 *List<DirectoryBean>
	 */
	public List<TbUserDirectory> getDirectryListByRole(String tiaojian ){
		List<TbUserDirectory> directoryList = new ArrayList();
		String condition = "";
		condition=tiaojian;
		directoryList = new AddressModuleDao().getUserDirByRole("tb_userDirectory","createDate",condition);
		return directoryList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get a Directory 
	 * @param tableName
	 * @param inKey
	 * @param keyValue
	 * @return
	 *2011-2-25
	 *上午10:46:13
	 *MmsModule
	 *com.AddressListModule.servlet
	 *DirectoryBean
	 */
	public DirectoryBean getDirectory(String tableName,String inKey,int keyValue){
		DirectoryBean dir = new AddressModuleDao().getDirectory(tableName, inKey,keyValue);
		return dir;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get common direcotry from tb_userDirectory
	 * @param sb
	 * @param parDirId
	 * @return
	 *2011-2-25
	 *上午10:51:49
	 *MmsModule
	 *com.AddressListModule.servlet
	 *String
	 */
	public String getDirStringByRole(StringBuffer sb,int parDirId,HttpServletRequest request){
		StringBuffer sssb = null;
		if(sb==null){
			sssb = new StringBuffer();
		}else{
			sssb = sb ;
		}
		TbUserBean tbUser = (TbUserBean)request.getSession().getAttribute("User");
		String condition = "parentDirId="+parDirId;
		List<DirectoryBean> dirList = new AddressModuleDao().getAllTbDirectory("tb_directory","createTime",condition);
		if(dirList!=null&&dirList.size()>0){
			sssb.append(" <ul class=\"treeview\">");
			for(DirectoryBean dir:dirList){
				//sbALL.append(" <ul class=\"treeview\">");
				sssb.append("<li><input type=\"checkbox\" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
				/*if(tbUser.getUserState()==0){//superadmin
					sssb.append("<li><input type=\"checkbox\" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
				}else{
					if(commonDirList.contains(dir)){
						sssb.append("<li><input type=\"checkbox\" name=\"select_Dir\" id=\""+dir.getDirId()+"\" onclick=\"ts(this);\" /><a>"+dir.getDirName()+"</a>");
					}
					
				}*/
				this.getDirStringByRole(sssb,dir.getDirId(),request);
				sssb.append("</li>");
			}
			sssb.append("</ul>");
		}else{
			//sbALL.append("</li>");
		}
		//sbALL.append("</li>");
		return sssb.toString();
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:所赋予权限中通讯录只有大于一级目录的存在
	 * @param commonDirList
	 * @param condition
	 * @return
	 *2011-3-1
	 *上午09:23:28
	 *MmsModule
	 *com.AddressListModule.servlet
	 *List<DirectoryBean>                  7  5
	 */
	public List<String> dirLst(List<DirectoryBean> commonDirList){
		List<String> lastDirLst = new ArrayList<String>();
		for(DirectoryBean db :commonDirList){
			lastDirLst.add(String.valueOf(db.getDirId()));
		}
		for(DirectoryBean directory:commonDirList){
			String condition ="parentDirId="+directory.getDirId();
			List<DirectoryBean> chilDirectory= this.getDirectryList(condition);
			if(chilDirectory!=null&&chilDirectory.size()>0){
				for(int v = 0;v<chilDirectory.size();v++){
					DirectoryBean chilDir = chilDirectory.get(v);
					if(lastDirLst.contains(String.valueOf(chilDir.getDirId()))){
						boolean bo   = lastDirLst.remove(String.valueOf(chilDir.getDirId()));
					}
				}
				
			}
		}
		return lastDirLst;
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		this.doGet(req, resp);
		
	}

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-2-17
	 *下午02:13:57
	 *AddressListModule
	 *com.AddressListModule.servlet
	 */
}
